Option Explicit On
Option Strict On

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Security

''' <summary>
''' This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
''' </summary>
''' <remarks>
''' The GUID attached to this class may be used during packaging and should not be modified.
''' </remarks>
Imports Microsoft.SharePoint.Administration

<Guid("90a55a45-8975-4ff6-b326-850cec22a2ef")> _
Public Class Feature1EventReceiver
    Inherits SPFeatureReceiver

    ' Uncomment the method below to handle the event raised after a feature has been activated.

    Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)
        Try


            'Dim aSite As SPSite = SPContext.Current.Site
            Const JobName As String = "CheckCPUUse_InstallCPUFeature"
            Dim anApp As SPWebApplication = CType(properties.Feature.Parent, SPWebApplication)
            For Each aJob As SPJobDefinition In anApp.JobDefinitions

                If aJob.Name = JobName Then

                    aJob.Delete()
                    '  aJob.Update()

                End If
            Next
            Dim aTimerJob As New CPUTimerJob(JobName, anApp, SPJobLockType.ContentDatabase)

            Dim aSchedule As New SPMinuteSchedule()
            aSchedule.BeginSecond = 0
            aSchedule.EndSecond = 59
            aSchedule.Interval = 2
            aTimerJob.Schedule = aSchedule
            aTimerJob.Update()








        Catch ex As Exception

            EventLog.WriteEntry("CPU Use App", ex.Message & ":" & ex.StackTrace)
            Throw (ex)

        End Try






    End Sub


    ' Uncomment the method below to handle the event raised before a feature is deactivated.

    Public Overrides Sub FeatureDeactivating(ByVal properties As SPFeatureReceiverProperties)
        Try

        
        Const JobName As String = "CheckCPUUse"

        Dim aSite As SPSite = CType(Microsoft.SharePoint.Administration.SPAdministrationWebApplication.Local, SPWebApplication).Sites(0)

        For Each aJob As SPJobDefinition In aSite.WebApplication.JobDefinitions

            If aJob.Name = JobName Then
                aJob.Delete()
            End If



        Next

        Catch ex As Exception
            EventLog.WriteEntry("CheckCPUUse", ex.Message & ":" & ex.StackTrace)

            Throw (ex)

        End Try

    End Sub


    ' Uncomment the method below to handle the event raised after a feature has been installed.

    'Public Overrides Sub FeatureInstalled(ByVal properties As SPFeatureReceiverProperties)
    'End Sub


    ' Uncomment the method below to handle the event raised before a feature is uninstalled.

    'Public Overrides Sub FeatureUninstalling(ByVal properties As SPFeatureReceiverProperties)
    'End Sub

    ' Uncomment the method below to handle the event raised when a feature is upgrading.

    'Public Overrides Sub FeatureUpgrading(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties, ByVal upgradeActionName As String, ByVal parameters As System.Collections.Generic.IDictionary(Of String, String))
    'End Sub

End Class
